package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Admin;
import com.j48.entrancegurad.model.Host;
import com.j48.entrancegurad.model.Repair;
import com.j48.entrancegurad.model.User;
import com.j48.entrancegurad.service.IAdminService;
import com.j48.entrancegurad.service.IHostService;
import com.j48.entrancegurad.service.IRepairService;
import com.j48.entrancegurad.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class RepairController {
    @Autowired
    private IRepairService repairservice;
    @Autowired
    private IUserService userService;
    @Autowired
    private IHostService hostService;
    @Autowired
    private IAdminService adminService;
    /**
     * 未处理完报修列表
     * @param request
     * @return
     */
    @RequestMapping("/repairListAble")
    @ResponseBody
    public ModelAndView repairListAble(HttpServletRequest request){
        ModelAndView model=null;
        List<Repair> list = new ArrayList<Repair>();
        list=repairservice.selectAllAble();
        List<User> userlist = new ArrayList<User>();
        List<Host> hostList=new ArrayList<Host>();
        List<Admin> adminList=new ArrayList<Admin>();
        if(list!=null) {
            List<Integer> user_idList = new ArrayList<Integer>();
            List<Integer> admin_idList=new ArrayList<Integer>();
            for (Repair r : list) {
                user_idList.add(r.getRepair_user_id());
                admin_idList.add(r.getRepair_admin_id());
            }
            for (int i : user_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                userlist.add(user);
            }
            for (User u:userlist) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostList.add(host);
            }
            for (int i:admin_idList) {
                Admin admin=new Admin();
                admin.setAdmin_id(i);
                admin=adminService.selectById(admin);
                adminList.add(admin);
            }
        }
        request.getSession().setAttribute("repairList", list);
        request.getSession().setAttribute("repairUserList", userlist);
        request.getSession().setAttribute("repairAdminList",adminList);
        request.getSession().setAttribute("repairHostList",hostList);
        model = new ModelAndView("jsp/repair");
        return model;
    }

    /**
     * 所有报修记录列表
     * @param request
     * @return
     */
    @RequestMapping("/repairList")
    @ResponseBody
    public ModelAndView repairList(HttpServletRequest request){
        ModelAndView model=null;
        List<Repair> list = new ArrayList<Repair>();
        list=repairservice.selectAll();
        List<User> userlist = new ArrayList<User>();
        List<Admin> adminList=new ArrayList<Admin>();
        List<Host> hostList=new ArrayList<Host>();
        if(list!=null) {
            List<Integer> user_idList = new ArrayList<Integer>();
            List<Integer> admin_idList=new ArrayList<Integer>();
            for (Repair r : list) {
                user_idList.add(r.getRepair_user_id());
                admin_idList.add(r.getRepair_admin_id());
            }
            for (int i : user_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                userlist.add(user);
            }
            for (User u:userlist) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostList.add(host);
            }
            for (int i:admin_idList) {
                Admin admin=new Admin();
                admin.setAdmin_id(i);
                admin=adminService.selectById(admin);
                adminList.add(admin);
            }
        }
        request.getSession().setAttribute("repairAllList", list);
        request.getSession().setAttribute("repairAllUserList", userlist);
        request.getSession().setAttribute("repairAllAdminList",adminList);
        request.getSession().setAttribute("repairAllHostList",hostList);
        model = new ModelAndView("jsp/repairAll");
        return model;
    }
    /**
     * 查询报修信息
     * @param repair_user_name 查询用户名
     * @param request
     * @return 跳转的页面
     */
    @RequestMapping("/selectRepair")
    @ResponseBody
    public ModelAndView selectRepair(String repair_user_name, HttpServletRequest request){
        if(null==repair_user_name||"".equals(repair_user_name)){
            return repairListAble(request);
        }
        ModelAndView model=null;
        User user=new User();
        user.setUser_name(repair_user_name);
        List<User> userList=new ArrayList<User>();
        userList=userService.selectByUser_name(user);
        List<Repair> lists = new ArrayList<Repair>();
        List<User> userList1=new ArrayList<User>();
        List<Host> hostList=new ArrayList<Host>();
        List<Admin> adminList=new ArrayList<>();
        if(userList!=null){
            for (User u:userList) {
                Repair repair=new Repair();
                repair.setRepair_user_id(u.getUser_id());
                List<Repair> repairList=new ArrayList<Repair>();
                repairList=repairservice.selectByUserAble(repair);
                if(repairList!=null){
                    List<Integer> user_idList = new ArrayList<Integer>();
                    List<Integer> admin_idList=new ArrayList<Integer>();
                    for (Repair r:repairList) {
                        lists.add(r);
                        user_idList.add(r.getRepair_user_id());
                        admin_idList.add(r.getRepair_admin_id());
                    }
                    for (int i : user_idList) {
                        User user2 = new User();
                        user2.setUser_id(i);
                        User user3 = userService.selectByUser_id(user2);
                        userList1.add(user3);
                    }
                    for (int i:admin_idList) {
                        Admin admin=new Admin();
                        admin.setAdmin_id(i);
                        admin=adminService.selectById(admin);
                        adminList.add(admin);
                    }
                }
            }
            for (User u:userList1) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                Host host1=hostService.selectById(host);
                hostList.add(host1);
            }
        }

        model = new ModelAndView("jsp/repair");
        request.getSession().setAttribute("repairList", lists);
        request.getSession().setAttribute("repairUserList", userList1);
        request.getSession().setAttribute("repairHostList", hostList);
        request.getSession().setAttribute("repairAdminList",adminList);
        return model;
    }
    /**
     * 查询报修信息
     * @param repair_user_name 查询用户名
     * @param request
     * @return 跳转的页面
     */
    @RequestMapping("/selectRepairall")
    @ResponseBody
    public ModelAndView selectRepairall(String repair_user_name, HttpServletRequest request){
        if(null==repair_user_name||"".equals(repair_user_name)){
            return repairList(request);
        }
        ModelAndView model=null;
        User user=new User();
        user.setUser_name(repair_user_name);
        List<User> userList=new ArrayList<User>();
        userList=userService.selectByUser_name(user);
        List<Repair> lists = new ArrayList<Repair>();
        List<User> userList1=new ArrayList<User>();
        List<Host> hostList=new ArrayList<Host>();
        List<Admin> adminList=new ArrayList<Admin>();
        if(userList!=null){
            for (User u:userList) {
                Repair repair=new Repair();
                repair.setRepair_user_id(u.getUser_id());
                List<Repair> repairList=new ArrayList<Repair>();
                repairList=repairservice.selectByUser(repair);
                if(repairList!=null){
                    List<Integer> user_idList = new ArrayList<Integer>();
                    List<Integer> admin_idList=new ArrayList<Integer>();
                    for (Repair r:repairList) {
                        lists.add(r);
                        user_idList.add(r.getRepair_user_id());
                        admin_idList.add(r.getRepair_admin_id());
                    }
                    for (int i : user_idList) {
                        User user2 = new User();
                        user2.setUser_id(i);
                        user2 = userService.selectByUser_id(user2);
                        userList1.add(user2);
                    }
                    for (int i:admin_idList) {
                        Admin admin=new Admin();
                        admin.setAdmin_id(i);
                        admin=adminService.selectById(admin);
                        adminList.add(admin);
                    }
                }
            }
            for (User u:userList1) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostList.add(host);
            }
        }
        model = new ModelAndView("jsp/repairAll");
        request.getSession().setAttribute("repairAllList", lists);
        request.getSession().setAttribute("repairAllUserList", userList1);
        request.getSession().setAttribute("repairAllAdminList",adminList);
        request.getSession().setAttribute("repairAllHostList", hostList);
        return model;
    }
    /**
     * 添加报修信息
     * @return 跳转的页面
     */
    @RequestMapping("/repairAddController")
    @ResponseBody
    public String visitorAdd(HttpServletRequest request){
        String repair_user_name=request.getParameter("repair_user_name");
        String repair_user_building=request.getParameter("repair_user_building");
        int repair_user_building1;
        try {
            repair_user_building1=Integer.parseInt(repair_user_building);
        }catch (Exception e){
            return "false";
        }
        String repair_user_unit=request.getParameter("repair_user_unit");
        int repair_user_unit1;
        try {
            repair_user_unit1=Integer.parseInt(repair_user_unit);
        }catch (Exception e){
            return "false";
        }
        String repair_user_housenumber=request.getParameter("repair_user_housenumber");
        int repair_user_housenunber1;
        try {
            repair_user_housenunber1=Integer.parseInt(repair_user_housenumber);
        }catch (Exception e){
            return "false";
        }
        String repair_describe=request.getParameter("repair_describe");
        if(null==repair_user_name||"".equals(repair_user_name)||null==repair_user_building||"".equals(repair_user_building)||null==repair_user_unit||"".equals(repair_user_unit)||null==repair_user_housenumber||"".equals(repair_user_housenumber)){
            return "false";
        }else {
            User user=new User();
            user.setUser_name(repair_user_name);
            List<User> userList=userService.selectByUser_name(user);
            if(userList!=null){
                for (User u:userList) {
                    Host host=new Host();
                    host.setHost_id(u.getUser_host_id());
                    host=hostService.selectById(host);
                    if(host.getHost_building()==repair_user_building1&&host.getHost_unit()==repair_user_unit1&&host.getHost_housenumber()==repair_user_housenunber1){
                        Repair repair=new Repair();
                        repair.setRepair_user_id(u.getUser_id());
                        repair.setRepair_describe(repair_describe);
                        Timestamp ts1 = new Timestamp(new Date().getTime());
                        repair.setRepair_createtime(ts1);
                        Timestamp ts = null;
                        boolean b=repairservice.repairAdd1(repair);
                        if(b){
                            flush(request);
                            return "true";
                        }
                    }
                }
            }
        }
        return "false";
    }

    /**
     * 修改未处理为处理中
     * @param request http请求
     * @return 是否成功
     */
    @RequestMapping("/modifyRepair")
    @ResponseBody
    public String modifyRepair(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("repair_id"));
        Repair rp=new Repair(id);
        Admin admin=(Admin)request.getSession().getAttribute("admin");
        Timestamp timestamp=new Timestamp(new Date().getTime());
        rp.setRepair_lasttime(timestamp);
        rp.setRepair_admin_id(admin.getAdmin_id());
        repairservice.repairmodify(rp);
        return "true";
    }
    /**
     * 修改处理中为处理完毕
     * @param request http请求
     * @return 是否成功
     */
    @RequestMapping("/overRepair")
    @ResponseBody
    public String overRepair(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("repair_id"));
        Repair rp=new Repair(id);
        Admin admin=(Admin)request.getSession().getAttribute("admin");
        Timestamp timestamp=new Timestamp(new Date().getTime());
        rp.setRepair_lasttime(timestamp);
        rp.setRepair_admin_id(admin.getAdmin_id());
        repairservice.repairover(rp);
        return "true";
    }

    /**
     * 修改报修操作
     * @param repair 对象
     * @return 跳转的页面，成功与否
     */
    @RequestMapping("/updateRepairController")
    @ResponseBody
    public ModelAndView updateRepairController(Repair repair){
        ModelAndView model=null;
        if (repairservice.repairUpdate(repair)){//判断修改是否成功
            model=new ModelAndView("jsp/success");//成功后跳转的页面
        }else {
            model=new ModelAndView("jsp/failure");//失败后跳转的页面
        }
        return model;
    }

    /**
     * 删除报修记录
     * @param request
     * @return
     */
    @RequestMapping("/deleteRepairController")
    @ResponseBody
    public String deleteRepairController(HttpServletRequest request){
        String repair_id=request.getParameter("repair_id");
        int id=Integer.parseInt(repair_id);
        Repair rp=new Repair(id);
        Repair rp1=repairservice.selectById(rp);
        if (repairservice.repairDelete(rp1)){
            return "true";
        }
        return "false";
    }

    /**
     * 更新报修记录
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/repairUpdate")
    @ResponseBody
    public ModelAndView repairUpdate(HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView=new ModelAndView("jsp/repairUpdate");
        return modelAndView;
    }

    /**
     * 刷新报修列表
     * @param req
     * @return
     */
    @RequestMapping("/repairflush")
    @ResponseBody
    public String repairflush(HttpServletRequest req){
        flush(req);
        return "true";
    }
    public void flush(HttpServletRequest request){
        request.getSession().removeAttribute("repairList");
        request.getSession().removeAttribute("repairUserList");
        request.getSession().removeAttribute("repairAdminList");
        request.getSession().removeAttribute("repairHostList");
        request.getSession().removeAttribute("repairAllList");
        request.getSession().removeAttribute("repairAllUserList");
        request.getSession().removeAttribute("repairAllAdminList");
        request.getSession().removeAttribute("repairAllHostList");
        List<Repair> list = new ArrayList<Repair>();
        list=repairservice.selectAllAble();
        List<User> userlist = new ArrayList<User>();
        List<Host> hostList=new ArrayList<Host>();
        List<Admin> adminList=new ArrayList<Admin>();
        if(list!=null) {
            List<Integer> user_idList = new ArrayList<Integer>();
            List<Integer> admin_idList=new ArrayList<Integer>();
            for (Repair r : list) {
                user_idList.add(r.getRepair_user_id());
                admin_idList.add(r.getRepair_admin_id());
            }
            for (int i : user_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                userlist.add(user);
            }
            for (User u:userlist) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostList.add(host);
            }
            for (int i:admin_idList) {
                Admin admin=new Admin();
                admin.setAdmin_id(i);
                admin=adminService.selectById(admin);
                adminList.add(admin);
            }
        }
        request.getSession().setAttribute("repairList", list);
        request.getSession().setAttribute("repairUserList", userlist);
        request.getSession().setAttribute("repairAdminList",adminList);
        request.getSession().setAttribute("repairHostList",hostList);
        List<Repair> alllist = new ArrayList<Repair>();
        alllist=repairservice.selectAll();
        List<User> alluserlist = new ArrayList<User>();
        List<Admin> alladminList=new ArrayList<Admin>();
        List<Host> allhostList=new ArrayList<Host>();
        if(alllist!=null) {
            List<Integer> user_idList = new ArrayList<Integer>();
            List<Integer> admin_idList=new ArrayList<Integer>();
            for (Repair r : alllist) {
                user_idList.add(r.getRepair_user_id());
                admin_idList.add(r.getRepair_admin_id());
            }
            for (int i : user_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                alluserlist.add(user);
            }
            for (User u:alluserlist) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                allhostList.add(host);
            }
            for (int i:admin_idList) {
                Admin admin=new Admin();
                admin.setAdmin_id(i);
                admin=adminService.selectById(admin);
                alladminList.add(admin);
            }
        }
        request.getSession().setAttribute("repairAllList", alllist);
        request.getSession().setAttribute("repairAllUserList", alluserlist);
        request.getSession().setAttribute("repairAllAdminList",alladminList);
        request.getSession().setAttribute("repairAllHostList",allhostList);
    }
}
